home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / iceweasel / modules / distribution.js < prev    next >
Encoding:
Text File  |  2013-01-09  |  13.4 KB  |  399 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Firefox Distribution Customizations.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla Foundation.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Dan Mills <thunder@mozilla.com>
  22.  *   Marco Bonardo <mak77@bonardo.net>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. EXPORTED_SYMBOLS = [ "DistributionCustomizer" ];
  39.  
  40. const Ci = Components.interfaces;
  41. const Cc = Components.classes;
  42. const Cr = Components.results;
  43. const Cu = Components.utils;
  44.  
  45. const DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC =
  46.   "distribution-customization-complete";
  47.  
  48. function DistributionCustomizer() {
  49.   let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
  50.                getService(Ci.nsIProperties);
  51.   let iniFile = dirSvc.get("XCurProcD", Ci.nsIFile);
  52.   iniFile.append("distribution");
  53.   iniFile.append("distribution.ini");
  54.   if (iniFile.exists())
  55.     this._iniFile = iniFile;
  56. }
  57.  
  58. DistributionCustomizer.prototype = {
  59.   _iniFile: null,
  60.  
  61.   get _ini() {
  62.     let ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
  63.               getService(Ci.nsIINIParserFactory).
  64.               createINIParser(this._iniFile);
  65.     this.__defineGetter__("_ini", function() ini);
  66.     return this._ini;
  67.   },
  68.  
  69.   get _locale() {
  70.     let locale;
  71.     try {
  72.       locale = this._prefs.getCharPref("general.useragent.locale");
  73.     }
  74.     catch (e) {
  75.       locale = "en-US";
  76.     }
  77.     this.__defineGetter__("_locale", function() locale);
  78.     return this._locale;
  79.   },
  80.  
  81.   get _bmSvc() {
  82.     let svc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
  83.               getService(Ci.nsINavBookmarksService);
  84.     this.__defineGetter__("_bmSvc", function() svc);
  85.     return this._bmSvc;
  86.   },
  87.  
  88.   get _annoSvc() {
  89.     let svc = Cc["@mozilla.org/browser/annotation-service;1"].
  90.               getService(Ci.nsIAnnotationService);
  91.     this.__defineGetter__("_annoSvc", function() svc);
  92.     return this._annoSvc;
  93.   },
  94.  
  95.   get _livemarkSvc() {
  96.     let svc = Cc["@mozilla.org/browser/livemark-service;2"].
  97.               getService(Ci.nsILivemarkService);
  98.     this.__defineGetter__("_livemarkSvc", function() svc);
  99.     return this._livemarkSvc;
  100.   },
  101.  
  102.   get _prefSvc() {
  103.     let svc = Cc["@mozilla.org/preferences-service;1"].
  104.               getService(Ci.nsIPrefService);
  105.     this.__defineGetter__("_prefSvc", function() svc);
  106.     return this._prefSvc;
  107.   },
  108.  
  109.   get _prefs() {
  110.     let branch = this._prefSvc.getBranch(null);
  111.     this.__defineGetter__("_prefs", function() branch);
  112.     return this._prefs;
  113.   },
  114.  
  115.   get _ioSvc() {
  116.     let svc = Cc["@mozilla.org/network/io-service;1"].
  117.               getService(Ci.nsIIOService);
  118.     this.__defineGetter__("_ioSvc", function() svc);
  119.     return this._ioSvc;
  120.   },
  121.  
  122.   _makeURI: function DIST__makeURI(spec) {
  123.     return this._ioSvc.newURI(spec, null, null);
  124.   },
  125.  
  126.   _parseBookmarksSection:
  127.   function DIST_parseBookmarksSection(parentId, section) {
  128.     let keys = [];
  129.     for (let i in enumerate(this._ini.getKeys(section)))
  130.       keys.push(i);
  131.     keys.sort();
  132.  
  133.     let items = {};
  134.     let defaultItemId = -1;
  135.     let maxItemId = -1;
  136.  
  137.     for (let i = 0; i < keys.length; i++) {
  138.       let m = /^item\.(\d+)\.(\w+)\.?(\w*)/.exec(keys[i]);
  139.       if (m) {
  140.         let [foo, iid, iprop, ilocale] = m;
  141.         iid = parseInt(iid);
  142.  
  143.         if (ilocale)
  144.           continue;
  145.  
  146.         if (!items[iid])
  147.           items[iid] = {};
  148.         if (keys.indexOf(keys[i] + "." + this._locale) >= 0) {
  149.           items[iid][iprop] = this._ini.getString(section, keys[i] + "." +
  150.                                                   this._locale);
  151.         } else {
  152.           items[iid][iprop] = this._ini.getString(section, keys[i]);
  153.         }
  154.  
  155.         if (iprop == "type" && items[iid]["type"] == "default")
  156.           defaultItemId = iid;
  157.  
  158.         if (maxItemId < iid)
  159.           maxItemId = iid;
  160.       } else {
  161.         dump("Key did not match: " + keys[i] + "\n");
  162.       }
  163.     }
  164.  
  165.     let prependIndex = 0;
  166.     for (let iid = 0; iid <= maxItemId; iid++) {
  167.       if (!items[iid])
  168.         continue;
  169.  
  170.       let index = this._bmSvc.DEFAULT_INDEX;
  171.       let newId;
  172.  
  173.       switch (items[iid]["type"]) {
  174.       case "default":
  175.         break;
  176.  
  177.       case "folder":
  178.         if (iid < defaultItemId)
  179.           index = prependIndex++;
  180.  
  181.         newId = this._bmSvc.createFolder(parentId, items[iid]["title"], index);
  182.  
  183.         this._parseBookmarksSection(newId, "BookmarksFolder-" +
  184.                                     items[iid]["folderId"]);
  185.  
  186.         if (items[iid]["description"])
  187.           this._annoSvc.setItemAnnotation(newId,
  188.                                           "bookmarkProperties/description",
  189.                                           items[iid]["description"], 0,
  190.                                           this._annoSvc.EXPIRE_NEVER);
  191.  
  192.         break;
  193.  
  194.       case "separator":
  195.         if (iid < defaultItemId)
  196.           index = prependIndex++;
  197.         this._bmSvc.insertSeparator(parentId, index);
  198.         break;
  199.  
  200.       case "livemark":
  201.         if (iid < defaultItemId)
  202.           index = prependIndex++;
  203.  
  204.         // Don't bother updating the livemark contents on creation.
  205.         newId = this._livemarkSvc.
  206.           createLivemarkFolderOnly(parentId,
  207.                                    items[iid]["title"],
  208.                                    this._makeURI(items[iid]["siteLink"]),
  209.                                    this._makeURI(items[iid]["feedLink"]),
  210.                                    index);
  211.         break;
  212.  
  213.       case "bookmark":
  214.       default:
  215.         if (iid < defaultItemId)
  216.           index = prependIndex++;
  217.  
  218.         newId = this._bmSvc.insertBookmark(parentId,
  219.                                            this._makeURI(items[iid]["link"]),
  220.                                            index, items[iid]["title"]);
  221.  
  222.         if (items[iid]["description"])
  223.           this._annoSvc.setItemAnnotation(newId,
  224.                                           "bookmarkProperties/description",
  225.                                           items[iid]["description"], 0,
  226.                                           this._annoSvc.EXPIRE_NEVER);
  227.  
  228.         break;
  229.       }
  230.     }
  231.   },
  232.  
  233.   _customizationsApplied: false,
  234.   applyCustomizations: function DIST_applyCustomizations() {
  235.     this._customizationsApplied = true;
  236.     if (!this._iniFile)
  237.       return this._checkCustomizationComplete();
  238.  
  239.     // nsPrefService loads very early.  Reload prefs so we can set
  240.     // distribution defaults during the prefservice:after-app-defaults
  241.     // notification (see applyPrefDefaults below)
  242.     this._prefSvc.QueryInterface(Ci.nsIObserver);
  243.     this._prefSvc.observe(null, "reload-default-prefs", null);
  244.   },
  245.  
  246.   _bookmarksApplied: false,
  247.   applyBookmarks: function DIST_applyBookmarks() {
  248.     this._bookmarksApplied = true;
  249.     if (!this._iniFile)
  250.       return this._checkCustomizationComplete();
  251.  
  252.     let sections = enumToObject(this._ini.getSections());
  253.  
  254.     // The global section, and several of its fields, is required
  255.     // (we also check here to be consistent with applyPrefDefaults below)
  256.     if (!sections["Global"])
  257.       return this._checkCustomizationComplete();
  258.     let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  259.     if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
  260.       return this._checkCustomizationComplete();
  261.  
  262.     let bmProcessedPref;
  263.     try {
  264.       bmProcessedPref = this._ini.getString("Global",
  265.                                             "bookmarks.initialized.pref");
  266.     }
  267.     catch (e) {
  268.       bmProcessedPref = "distribution." +
  269.         this._ini.getString("Global", "id") + ".bookmarksProcessed";
  270.     }
  271.  
  272.     let bmProcessed = false;
  273.     try {
  274.       bmProcessed = this._prefs.getBoolPref(bmProcessedPref);
  275.     }
  276.     catch (e) {}
  277.  
  278.     if (!bmProcessed) {
  279.       if (sections["BookmarksMenu"])
  280.         this._parseBookmarksSection(this._bmSvc.bookmarksMenuFolder,
  281.                                     "BookmarksMenu");
  282.       if (sections["BookmarksToolbar"])
  283.         this._parseBookmarksSection(this._bmSvc.toolbarFolder,
  284.                                     "BookmarksToolbar");
  285.       this._prefs.setBoolPref(bmProcessedPref, true);
  286.     }
  287.     return this._checkCustomizationComplete();
  288.   },
  289.  
  290.   _prefDefaultsApplied: false,
  291.   applyPrefDefaults: function DIST_applyPrefDefaults() {
  292.     this._prefDefaultsApplied = true;
  293.     if (!this._iniFile)
  294.       return this._checkCustomizationComplete();
  295.  
  296.     let sections = enumToObject(this._ini.getSections());
  297.  
  298.     // The global section, and several of its fields, is required
  299.     if (!sections["Global"])
  300.       return this._checkCustomizationComplete();
  301.     let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  302.     if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
  303.       return this._checkCustomizationComplete();
  304.  
  305.     let defaults = this._prefSvc.getDefaultBranch(null);
  306.  
  307.     // Global really contains info we set as prefs.  They're only
  308.     // separate because they are "special" (read: required)
  309.  
  310.     defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
  311.     defaults.setCharPref("distribution.version",
  312.                          this._ini.getString("Global", "version"));
  313.  
  314.     let partnerAbout = Cc["@mozilla.org/supports-string;1"].
  315.       createInstance(Ci.nsISupportsString);
  316.     if (globalPrefs["about." + this._locale]) {
  317.       partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
  318.     } else {
  319.       partnerAbout.data = this._ini.getString("Global", "about");
  320.     }
  321.     defaults.setComplexValue("distribution.about",
  322.                              Ci.nsISupportsString, partnerAbout);
  323.  
  324.     if (sections["Preferences"]) {
  325.       for (let key in enumerate(this._ini.getKeys("Preferences"))) {
  326.         try {
  327.           let value = eval(this._ini.getString("Preferences", key));
  328.           switch (typeof value) {
  329.           case "boolean":
  330.             defaults.setBoolPref(key, value);
  331.             break;
  332.           case "number":
  333.             defaults.setIntPref(key, value);
  334.             break;
  335.           case "string":
  336.             defaults.setCharPref(key, value);
  337.             break;
  338.           case "undefined":
  339.             defaults.setCharPref(key, value);
  340.             break;
  341.           }
  342.         } catch (e) { /* ignore bad prefs and move on */ }
  343.       }
  344.     }
  345.  
  346.     // We eval() the localizable prefs as well (even though they'll
  347.     // always get set as a string) to keep the INI format consistent:
  348.     // string prefs always need to be in quotes
  349.  
  350.     let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"].
  351.       createInstance(Ci.nsIPrefLocalizedString);
  352.  
  353.     if (sections["LocalizablePreferences"]) {
  354.       for (let key in enumerate(this._ini.getKeys("LocalizablePreferences"))) {
  355.         try {
  356.           let value = eval(this._ini.getString("LocalizablePreferences", key));
  357.           value = value.replace("%LOCALE%", this._locale, "g");
  358.           localizedStr.data = "data:text/plain," + key + "=" + value;
  359.           defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  360.         } catch (e) { /* ignore bad prefs and move on */ }
  361.       }
  362.     }
  363.  
  364.     if (sections["LocalizablePreferences-" + this._locale]) {
  365.       for (let key in enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
  366.         try {
  367.           let value = eval(this._ini.getString("LocalizablePreferences-" + this._locale, key));
  368.           localizedStr.data = "data:text/plain," + key + "=" + value;
  369.           defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  370.         } catch (e) { /* ignore bad prefs and move on */ }
  371.       }
  372.     }
  373.  
  374.     return this._checkCustomizationComplete();
  375.   },
  376.  
  377.   _checkCustomizationComplete: function DIST__checkCustomizationComplete() {
  378.     let prefDefaultsApplied = this._prefDefaultsApplied || !this._iniFile;
  379.     if (this._customizationsApplied && this._bookmarksApplied &&
  380.         prefDefaultsApplied) {
  381.       let os = Cc["@mozilla.org/observer-service;1"].
  382.                getService(Ci.nsIObserverService);
  383.       os.notifyObservers(null, DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC, null);
  384.     }
  385.   }
  386. };
  387.  
  388. function enumerate(UTF8Enumerator) {
  389.   while (UTF8Enumerator.hasMore())
  390.     yield UTF8Enumerator.getNext();
  391. }
  392.  
  393. function enumToObject(UTF8Enumerator) {
  394.   let ret = {};
  395.   for (let i in enumerate(UTF8Enumerator))
  396.     ret[i] = 1;
  397.   return ret;
  398. }
  399.